<?php

/**
 * 这个是客户购买的快递查询接口，
 * 购买链接 https://market.aliyun.com/products/56928004/cmapi023201.html#sku=yuncode1720100000
 * 服务商为：四川涪擎大数据技术有限公司
 * 本例实现3个接口：1快递查询 ，2物流公司查询 3，单号识别
 * @author 晓风<215628355@qq.com>
 */
namespace com;
class Kuaidi{
    
    
    const HOST = "https://wdexpress.market.alicloudapi.com";
   
    protected static $appcode = null;
    
    
    //设置APPcode
    public static function setAppcode($appcode){
        self::$appcode  = $appcode;
    }


    /**
     * 查询快递
     * @param string $sn 运单号
     * @param string $company 公司类型
     * @param string $mobile  顺丰手机号后四位
     */
    public static function query($sn,$company="",$mobile=""){
       
        if($mobile){       
            $arr["n"] = $sn .":" . mb_substr($mobile,-4) ;
        }else{
            $arr["n"] = $sn ;
        }        
        if($company){
            $arr["t"] = $company;
        }
        $querys = http_build_query($arr);        
        return self::curl("/gxali",$querys);
    }
    
    /**
     * 查询物流公司
     * @param string $company 公司类型 
     */
    public static function company($company=""){
        $arr["t"] = $company;
        $querys = http_build_query($arr);        
        return self::curl("/globalExpressLists",$querys);
    }
    
      /**
     * 根据单号识别快递公司
     * @param string $sn 运单号
     */
    public static function autocompany($sn){
        $arr["no"] = $sn;
        $querys = http_build_query($arr);        
        return self::curl("/exCompany",$querys);
    }
     //发送请求
    private static function curl($path,$querys ){    

        $url = self::HOST . $path . "?" . $querys;
        $headers[] = "Authorization:APPCODE " . self::$appcode;
            
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET");
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, true);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        $out_put = curl_exec($curl);
        $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
        list($header, $body) = explode("\r\n\r\n", $out_put, 2);

        if ($httpCode == 200) {              
            return json_decode($body,true);
        } 
        if ($httpCode == 400 && strpos($header, "Invalid Param Location") !== false) {
            throw new \Exception("参数错误");
        } 
        if ($httpCode == 400 && strpos($header, "Invalid AppCode") !== false) {
             throw new \Exception("AppCode错误");
        } 
        If ($httpCode == 400 && strpos($header, "Invalid Url") !== false) {
             throw new \Exception("请求的 Method、Path 或者环境错误");
        } 
        if ($httpCode == 403 && strpos($header, "Unauthorized") !== false) {
             throw new \Exception("服务未被授权（或URL和Path不正确）");
        } 
        if ($httpCode == 403 && strpos($header, "Quota Exhausted") !== false) {
             throw new \Exception("套餐包次数用完");
        } 
        if ($httpCode == 500) {
             throw new \Exception("API网关错误");
        } 
        if ($httpCode == 0) {
             throw new \Exception("URL错误");
        } 
        throw new \Exception("参数名错误 或 其他错误");
    }
}